(* This file is part of IsoType.
 *
 * IsoType is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * IsoType is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IsoType.  If not, see <http://www.gnu.org/licenses/>.
 *)

let rec string_of_type =
  function 
      Ast.Ref (typs, qname) -> string_of_reference typs qname
    | Ast.Var tvar -> string_of_tvar tvar
    | Ast.Arrow (typ1, typ2) ->
	"(" ^ string_of_type typ1 ^ " -> " ^ string_of_type typ2 ^ ")"
    | Ast.Tuple typs -> 
	"(" ^ String.concat ", " (List.map string_of_type typs) ^ ")"

and string_of_tvar tvar = "'" ^ tvar

and string_of_reference typs qname =
  match typs with
      [] -> string_of_qname qname
    | [typ] -> string_of_type typ ^ " " ^ string_of_qname qname
    | _ -> "(" ^ String.concat "," (List.map string_of_type typs) ^ ") " ^ string_of_qname qname

and string_of_qname =
  function
      Ast.Id name -> name
    | Ast.Segment (segment, qname) -> segment ^ "." ^ string_of_qname qname
    | Ast.Functor (segment, mdule, qname) -> segment ^ "(" ^ mdule ^ ")." ^ string_of_qname qname

let string_of_field (Ast.Field (name, typ)) =
  name ^ " : " ^ string_of_type typ

let string_of_constructor = 
  function 
      Ast.Constructor (name, []) -> name
    | Ast.Constructor (name, typs) -> name ^ " of " ^ String.concat " * " (List.map string_of_type typs)

let string_of_type_body =
  function
      Ast.Abstract -> ""
    | Ast.Alias typ -> " = " ^ string_of_type typ
    | Ast.Algebraic ctors -> " = " ^ String.concat "\n  | " (List.map string_of_constructor ctors)
    | Ast.Record fields -> "{" ^ String.concat "; " (List.map string_of_field fields) ^ "}"

let string_of_formal_params =
  function
      [] -> ""
    | [tvar] -> string_of_tvar tvar ^ " "
    | tvars -> "(" ^ String.concat ", " (List.map string_of_tvar tvars) ^ ") "

(* ... *)

let string_of_type_decl (tvars, name, body) =
  (string_of_formal_params tvars) ^ name ^ (string_of_type_body body)
	  
let string_of_type_decls decls =
  "type " ^ String.concat "\nand " (List.map string_of_type_decl decls)

let string_of_value_decl name typ =
  "val " ^ name ^ " : " ^ string_of_type typ

let string_of_exception_decl ctor =
  "exception " ^ string_of_constructor ctor

let rec pp_entity = 
  function
      Ast.Type decls -> print_endline (string_of_type_decls decls)
    | Ast.Value (name, typ) -> print_endline (string_of_value_decl name typ)
    | Ast.Exception ctor -> print_endline (string_of_exception_decl ctor)
    | Ast.Module (name, entities) -> 
	Printf.printf "module %s : sig\n" name;
	List.iter pp_entity entities;
	Printf.printf "end\n"

and pp_type_expr typ = print_string (string_of_type typ)

and pp_val_dec (name, typ) =
  Printf.printf "val %s : %s" name (string_of_type typ)

(*SIMBOL TABLES PRETTY PRINTING*)

let pp_values hst = 
  let aux name typ = 
    Printf.printf "val %s : %s\n" (string_of_qname name) (string_of_type typ)
  in 
    Hashtbl.iter aux hst

let pp_types hst= 
  let aux name (tvars, body) = 
    Printf.printf "type %s%s%s\n"
      (string_of_formal_params tvars)
      (string_of_qname name)
      (string_of_type_body body)
  in
    Hashtbl.iter aux hst
